"""
RAG相关API
简历入库、智能检索等功能
"""
from flask import Blueprint, request, jsonify
from rag_system.vector_store import VectorStore
from rag_system.retriever import RAGRetriever

rag_bp = Blueprint('rag', __name__, url_prefix='/api/rag')
vector_store = VectorStore()
rag_retriever = RAGRetriever(vector_store)

@rag_bp.route('/add_resume', methods=['POST'])
def add_resume():
    data = request.get_json()
    resume_id = data.get('resume_id')
    content = data.get('content')
    metadata = data.get('metadata', {})
    if not resume_id or not content:
        return jsonify({'error': 'resume_id和content为必填'}), 400
    vector_store.add_resume(resume_id, content, metadata)
    return jsonify({'message': '简历已入库'}), 201

@rag_bp.route('/search_jobs', methods=['POST'])
def search_jobs():
    data = request.get_json()
    content = data.get('content')
    n_results = int(data.get('n_results', 5))
    if not content:
        return jsonify({'error': 'content为必填'}), 400
    results = rag_retriever.retrieve_job_matches(content, n_results)
    return jsonify({'results': results})

@rag_bp.route('/search_skills', methods=['POST'])
def search_skills():
    data = request.get_json()
    content = data.get('content')
    n_results = int(data.get('n_results', 10))
    if not content:
        return jsonify({'error': 'content为必填'}), 400
    results = rag_retriever.retrieve_skill_matches(content, n_results)
    return jsonify({'results': results})

@rag_bp.route('/stats', methods=['GET'])
def rag_stats():
    stats = rag_retriever.get_retrieval_stats()
    return jsonify({'stats': stats}) 